import Router from '@koa/router';
import UserController from '@/controllers/UserController';
import { auth, requireTenantAdmin, requirePermission } from '@/middlewares';
import { PermissionAction } from '@/types';

const router = new Router({
  prefix: '/api/users',
});

/**
 * 用户管理路由
 */

// 获取用户列表
router.get(
  '/',
  auth,
  requirePermission('user', PermissionAction.READ),
  UserController.list.bind(UserController),
);

// 创建用户
router.post(
  '/',
  auth,
  requirePermission('user', PermissionAction.CREATE),
  UserController.create.bind(UserController),
);

// 获取用户详情
router.get(
  '/:id',
  auth,
  requirePermission('user', PermissionAction.READ),
  UserController.getById.bind(UserController),
);

// 更新用户
router.put(
  '/:id',
  auth,
  requirePermission('user', PermissionAction.UPDATE),
  UserController.update.bind(UserController),
);

// 删除用户
router.delete(
  '/:id',
  auth,
  requirePermission('user', PermissionAction.DELETE),
  UserController.delete.bind(UserController),
);

// 批量删除用户
router.post(
  '/batch-delete',
  auth,
  requirePermission('user', PermissionAction.DELETE),
  UserController.batchDelete.bind(UserController),
);

// 激活用户
router.post(
  '/:id/activate',
  auth,
  requireTenantAdmin,
  UserController.activate.bind(UserController),
);

// 禁用用户
router.post(
  '/:id/deactivate',
  auth,
  requireTenantAdmin,
  UserController.deactivate.bind(UserController),
);

// 重置密码
router.post(
  '/:id/reset-password',
  auth,
  requireTenantAdmin,
  UserController.resetPassword.bind(UserController),
);

// 分配角色
router.post(
  '/:id/assign-role',
  auth,
  requireTenantAdmin,
  UserController.assignRole.bind(UserController),
);

// 批量导入用户
router.post(
  '/batch-import',
  auth,
  requirePermission('user', PermissionAction.CREATE),
  UserController.batchImport.bind(UserController),
);

// 获取导出数据
router.get(
  '/export-data',
  auth,
  requirePermission('user', PermissionAction.READ),
  UserController.getExportData.bind(UserController),
);

// 获取导入模板配置
router.get(
  '/import-template',
  auth,
  requirePermission('user', PermissionAction.READ),
  UserController.getImportTemplate.bind(UserController),
);

export default router;

